@file:OptIn(ExperimentalMaterial3Api::class)
package com.wxn.learncompose.pagers
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemColors
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun ScaffoldCardPager() {
    var selectedIndex = remember { mutableStateOf(0) }

    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.TopStart
    ){
        val titleBar:  @Composable ()->Unit = {TitleBar() }
        val bottomBar : @Composable ()->Unit = {BottomNavigator(selectedIndex) }
        Scaffold(
            modifier = Modifier.fillMaxSize(),
            topBar = titleBar,
            bottomBar = bottomBar,
        ){paddings ->
            Column(modifier = Modifier.padding(paddings)) {}
            when(selectedIndex.value) {
                0 -> Home()
                1 -> Favorite()
                2 -> Setting()
                else -> Home()
            }
        }
    }
}

@Composable fun TitleBar() {
    TopAppBar(title = { Text(text = "Title",
        modifier = Modifier.fillMaxWidth(),
        textAlign = TextAlign.Center)},
        navigationIcon = {
            IconButton(onClick = {

            }) {
                Icon(Icons.Default.Menu, "drawerout")
            }
        },
        actions = {
            IconButton(onClick = { /*TODO*/ }) {
                Icon(Icons.Default.Search, null)
            }
            IconButton(onClick = { /*TODO*/ }) {
                Icon(Icons.Default.Info, null)
            }
        }
    )
}

@Composable fun BottomNavigator(selectedIndex: MutableState<Int>) {
    val items = remember{ mutableListOf("主页" to Icons.Default.Home,
        "我喜欢的" to Icons.Default.Favorite,
        "设置" to Icons.Default.Settings) }

    NavigationBar {
        items.forEachIndexed{ index, item ->
            NavigationBarItem(selected = if (selectedIndex.value == index) true else false,
                onClick = { selectedIndex.value = index },
                label = {
                    Text(text=items[index].first)
                },
                icon = {
                    Icon(imageVector = items[index].second, "home")
                },
                colors = NavigationBarItemDefaults.colors()
            )
        }
    }
}

@Composable fun Home(){
    Box(modifier = Modifier
        .fillMaxSize()
        .background(Color.White),
        contentAlignment = Alignment.Center){
        Text(text = "home")
    }
}

@Composable fun Favorite() {
    Box(modifier = Modifier
        .fillMaxSize()
        .background(Color.LightGray),
        contentAlignment = Alignment.Center){
        Text(text = "favorite")
    }
}

@Composable fun Setting(){
    Box(modifier = Modifier
        .fillMaxSize()
        .background(Color(0xFFDDEEFF)),
        contentAlignment = Alignment.Center){
        Text(text = "setting")
    }
}

@Preview
@Composable
fun previewScaffoldCardPager() {
    ScaffoldCardPager()
}